package com.application.race.manage;

import java.util.Iterator;

import com.application.item.manage.ItemManage;
import com.application.race.manage.player.Player;

public class AwardManager {

      public static void makeAward(RacingRoom room){
    	  if(room.mode==0){
    		  makeAward0(room);
    	  }
    	  if(room.mode==1){
    		  makeAward1(room);
    	  }
      }
      private static void makeAward0(RacingRoom room){
        	int totalLv=0;
            
            	
            double totalPoint=0;
            Player[] sortedPlayers=new Player[room.players.size()];
            int i=0;
    		for(Iterator<Player> it = room.players.values().iterator(); it.hasNext();)
    		{
    			
    			Player player = it.next();
    			sortedPlayers[i]=player;
    			i++;
    			player.racePoint=player.totalDamage/40.0+player.totalKills*20;
    			totalPoint+=player.racePoint;
    			totalLv+=player.actor.getLevel();
				//player.result.castItems = player.castItems;

        	}
    		int totalPalyer=room.players.size();
    		sortPlayer(sortedPlayers);
    		if(totalPoint==0)totalPoint=1;
    		int income=room.getRaceIncome();
    		int totalBonus=(int)(income*0.5);
    		int SystemIncome=(int)(income*0.2);
    		int awardValue=(int)(income*0.3*DEF.BaseBonusCft);
    		cacuBonusAwardExp(room,sortedPlayers, totalLv,totalPalyer,totalBonus,totalPoint,awardValue);
    		rundomAwardPart(room,sortedPlayers,SystemIncome);
    	 

//    		for(i=0;i<sortedPlayers.length;i++)
//    		{
//    			Player player = sortedPlayers[i];
// 			
//    		    room.stocks.add(player.result);
//    		}
      }
      private static void makeAward1(RacingRoom room){
      	int totalLv=0;
    	int team_0No=0;
    	int team_1No=0;
		for(Iterator<Player> it = room.players.values().iterator(); it.hasNext();)
		{
			Player player = it.next();
			if(player.teamId==0)team_0No++;
			if(player.teamId==1)team_1No++;
    	}  
        	
        double totalPoint_team0=0;
        double totalPoint_team1=0;
        Player[] sortedPlayers_team0=new Player[team_0No];
        Player[] sortedPlayers_team1=new Player[team_1No];
        int i=0;
        int j=0;
		for(Iterator<Player> it = room.players.values().iterator(); it.hasNext();)
		{
			 
			Player player = it.next();
			//player.result.castItems = player.castItems;
			if(player.teamId==0){
				sortedPlayers_team0[i]=player;
				i++;
				player.racePoint=player.totalDamage/40.0+player.totalKills*20;
				totalPoint_team0+=player.racePoint;
			}
			if(player.teamId==1){
				sortedPlayers_team1[j]=player;
				j++;
				player.racePoint=player.totalDamage/40.0+player.totalKills*20;
				totalPoint_team1+=player.racePoint;
			}
			totalLv+=player.actor.getLevel();
			
    	}
		int totalPalyer=room.players.size();
		Player[] winner;
		int totalPoint=0;
		if(room.team0Win && !room.team1Win){
			winner=sortedPlayers_team0;
			totalPoint=(int)totalPoint_team0;
		}else if(room.team1Win && !room.team0Win){
			winner=sortedPlayers_team1;
			totalPoint=(int)totalPoint_team1;
		}else{
			if(totalPoint_team0>totalPoint_team1){
				winner=sortedPlayers_team0;
				totalPoint=(int)totalPoint_team0;
			}else{
				winner=sortedPlayers_team1;
				totalPoint=(int)totalPoint_team1;
			}
		}
		sortPlayer(winner);
		if(totalPoint==0)totalPoint=1;
		int income=room.getRaceIncome();
		int totalBonus=(int)(income*0.5);
		int SystemIncome=(int)(income*0.2);
		int awardValue=(int)(income*0.3*DEF.BaseBonusCft);
		cacuBonusAwardExp(room,winner, totalLv,totalPalyer,totalBonus,totalPoint,awardValue);
		rundomAwardPart(room,winner,SystemIncome);
	 

//		for(i=0;i<winner.length;i++)
//		{
//			Player player = winner[i];
//		    room.stocks.add(player.result);
//		}  
      }
      private static void sortPlayer(Player[] players){
    	  for(int i=0;i<players.length-1;i++){
    		  for(int j=i+1;j<players.length;j++){
    			  if(players[i].racePoint<players[j].racePoint){
    				  Player p=players[i];
    				  players[i]=players[j];
    				  players[j]=p;
    			  }
    		  }
    	  }
      }
      
      private static void cacuBonusAwardExp(RacingRoom room,Player[] sortedPlayers,int totalLv,int totalPalyer,int totalBonus,double totalPoint,int awardValue){
        int i=0;
        int totalPlayer=sortedPlayers.length;
        if(totalPlayer<=0)return;
		int itemNo=ItemManage.items.size();
		int rundomAward=DEF.random(0, itemNo-1);
		int rundomAwardType=(Integer)(ItemManage.items.keySet().toArray()[rundomAward]);
		int itemPrice=ItemManage.getItem(rundomAwardType,"cacuBonusAwardExp").saleValue;
		int totalAwardItemQtt=(int)(awardValue*1.0/itemPrice);
		double levelAdds=((totalLv/totalPalyer)*0.05+1.0);
  		for(i=0;i<sortedPlayers.length;i++)
		{
			Player player = sortedPlayers[i];
				int Lv=player.actor.getLevel();
				int otherTotalLv=0;
				int otherAverageLv=0;
				if(totalPlayer<=1){
					 
					 otherAverageLv=Lv;
				}else{
				     otherTotalLv=totalLv-Lv;
					 otherAverageLv=(int)(Math.ceil(otherTotalLv/(totalPlayer-1.0)));
				}
				
				player.bonus=(int)(totalBonus*(player.racePoint/totalPoint));
				player.exp=(int)(player.racePoint*levelAdds);
				
				player.result.awardItemQtt=(int)(Math.floor(totalAwardItemQtt*(player.racePoint/totalPoint)));
				if(player.result.awardItemQtt>0)player.result.awardItemType=rundomAwardType;
				
				if(Lv<=otherAverageLv){
                  
				}else{

					int lvGap=Lv-otherAverageLv;
					double pct=0;
					//20100212 cuij modifed
					if(lvGap>=10){
						pct=0.4;
					}else{
						pct=(10.0-lvGap)*0.06+0.4;
					}
					/*
					 
					if(lvGap>=10){
						pct=0.1;
					}else{
						pct=(10.0-lvGap)*0.09+0.1;
					}*/
					player.bonus*=pct;
					player.exp*=pct;
					
				}

				player.result.bonus+=player.bonus;
				player.result.exp+=player.exp;

    	}   
      }
      
      private static void rundomAwardPart(RacingRoom room,Player[] sortedPlayers,int SystemIncome){
 // 		if(SystemIncome<100){
//			return;
//		} 
  		boolean hadCar=true;
  		double awardCarChance=SystemIncome/100.0*DEF.BaseOddsCft;
  		double awardPartChance=SystemIncome*10/100.0*DEF.BaseOddsCft;
  		for(int i=0;i<sortedPlayers.length;i++){
  			 int chance=DEF.random(0, 1000);
  			 if(hadCar && chance<awardCarChance){
  				int itemNo=ItemManage.cars.size();
  				int rundomAward=DEF.random(0, itemNo-1);
  				int rundomAwardType=(Integer)(ItemManage.cars.keySet().toArray()[rundomAward]);
  				int odds=ItemManage.getItem( rundomAwardType,"rundomAwardPart").odds;
  				hadCar=false;
  				if(DEF.random(0, 100)<odds){
  					sortedPlayers[i].result.awardPartType=rundomAwardType;
  					continue;
  				}
  			 }else{
  				hadCar=false;
  			 }
  			 chance=DEF.random(0, 1000);
  			 if(!hadCar  && chance<awardPartChance){
   				int itemNo=ItemManage.parts.size();
   				int rundomAward=DEF.random(0, itemNo-1);
   				int rundomAwardType=(Integer)(ItemManage.parts.keySet().toArray()[rundomAward]);
   				int odds=ItemManage.getItem( rundomAwardType,"rundomAwardPart").odds;
   				if(DEF.random(0, 100)<odds){
   					sortedPlayers[i].result.awardPartType=rundomAwardType;
   				}else{
   					return;
   				}
   			 }else{
   				return;
   			 }
  			awardCarChance/=2;
  			awardPartChance/=2;
  		}
      }
}
